package top.glkj.hms.config;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import top.glkj.hms.exception.DefineException;
import top.glkj.hms.utils.Result;
import top.glkj.hms.utils.ResultFactory;
import top.glkj.hms.utils.StaticValue;

/**
 * 全局异常拦截处理
 * @author xgl
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(value = DefineException.class)
    @ResponseBody
    public Result bizExceptionHandler(DefineException e) {
        return ResultFactory.isException(e.getMessage(),e);
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result exceptionHandler(Exception e) {
        if (e instanceof NullPointerException){
            return ResultFactory.isError(StaticValue.ERROR_MESSAGE);
        }
        return ResultFactory.isError(e.getMessage(),e);
    }
}